<template>
  <div class="tabble_ground">
    <div style="margin-bottom: 40px;font-size: 18px;font-weight: 530;"><h3>管理员管理</h3></div>
    <div style="margin-bottom: 20px; display: flex">
      <div  @keyup.enter="load" style="flex: 10;text-align: left">
        <el-input placeholder="请输入查询内容" size="small"  v-model="search.userName" style="width: 20%; margin-right: 20px"><i slot="suffix" class="el-input__icon el-icon-search"></i></el-input>
        <el-button type="primary" size="small" style="border-radius: 1px;width: 100px;text-align: center" @click="load">查询</el-button>
        <el-button type="danger" size="small" style="border-radius: 1px;width: 100px;text-align: center" @click="reForm">重置</el-button>
      </div>
      <div style="flex:2;text-align: right">
        <el-button type="success" size="small" style="border-radius: 1px;width: 100px;text-align: center" @click="add">新增</el-button>
      </div>
      <!--   新增用户表单组件   -->
        <Dialog
        :dialogVisible="ParentDialogVisible"
        :form="childForm"
        @close="changeVisible"
      />
    </div>
    <el-table :data="tableData" border style="width: 100%" :fit="true" stripe :cell-style="{ textAlign: 'center' }" :header-cell-style="{ textAlign: 'center' }">
      <el-table-column prop="id" label="ID" ></el-table-column>
      <el-table-column label="头像">
        <template v-slot="scope">
          <el-image :src="scope.row.avatar" :preview-src-list="[scope.row.avatar]" style="width: 50px; height: 50px; border-radius: 50%"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="userName" label="姓名"></el-table-column>
      <el-table-column prop="phone" label="手机号"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="gender" label="性别"></el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
          <el-button type="text" size="small" @click="edit(scope.row)">编辑</el-button>
          <el-popconfirm title="确定删除吗？" @confirm="del(scope.row.id)" style="margin-left: 10px">
            <el-button size="small" type="text" slot="reference">删除</el-button>
          </el-popconfirm>
          <el-form>
            <el-form-item label="头像">
              <el-upload
                  class="avatar-uploader"
                  action="http://localhost:9090/files/upload"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess">
                <img v-if="childForm.avatar" :src="childForm.avatar" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="text-align: right;margin-top: 50px">
      <el-pagination
          background
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-size="5"
          layout="total, prev, pager, next"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import Dialog from "@/components/Dialog.vue"

export default {
  components:{
    Dialog
  },
//当页面加载时自动调用 vue 生命周期
  created() {
    //调用方法
    this.load()
    console.log(this.tableData)
  },
  methods: {
    //该方法的作用就获取后端传到前端的数据
    load(){
      //get 获取数据 put 修改 delte删除
      // request.get("/admin/alldata")
      request.post("/admin/page?pageNum=" + this.pageNum, this.search).then( res =>{
        console.log(res)
        // 请求成功
        if (res.code === '0'){
          // this.tableData = res.data
          this.tableData = res.data.list;
          this.total = res.data.total;
          // 把总的数据保存到缓存中去。
          // console.log("admin的数量",this.total);
        }else{
          //   请求失败 打印失败信息
          this.$notify.error(res.msg);
        }
      })
    },
    add(){
      // this.form = {};
      this.ParentDialogVisible = true;
    },
    // 删除行数据的方法
    del(id) {
      request.delete("/admin/" + id).then(res => {
        if (res.code === '0') {
          this.$notify.success('删除成功');
          this.load();
        } else {
          this.$notify.error(res.msg);
        }
      });
    },
    // 编辑行数据的方法
    edit(row){
      this.ParentDialogVisible = true;
      this.childForm = row;
    },
    reForm(){
      this.search = {};
    },
    changeVisible(){
      this.ParentDialogVisible = false;
    },
    handleAvatarSuccess(res){
      this.childForm.avatar = res.data;
    },
    handleClick(row) {
      console.log(row);
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(pageNum) {
      // console.log(`当前页: ${val}`);
      this.pageNum = pageNum;
      this.load();
    },

  },
  data() {
    return {
      ParentDialogVisible:false,
      childForm:{},
      input:'',
      pageNum:1,
      total:0,
      tableData: [],
      search:{},
    }
  }
}
</script>